<?php $this->load->view('includes/head.php'); ?>

<?php // var_dump($tasks); ?>
<!-- </pre> -->
<div class="container">
  <div class="row">
    <div class="col-sm-12">
      <table class="table table-hover table-striped">
        <thead>
          <tr>
            <th>#</th>
            <th>Task</th>
            <th>RACI Role(s)</th>
            <th>Priority</th>
            <th>Due Date</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody>
          <?php
          $count = 0;
          date_default_timezone_set('GMT');

          if(count($tasks) > 0){
            foreach ($tasks as $task) {

              $title = $task->title;
              $url = site_url('tasks/'.$task->url);

              $view_task_url = site_url('tasks/'.$task->url);
              $delete_task_url = site_url('tasks/delete/'.$task->url);
              $edit_task_url = site_url('tasks/edit/'.$task->url);
              $content = $task->content;
              $count++;
              
              $responsible = (in_array($task->user_id, json_decode($task->responsible))) ? '<em><span class="levels">Responsible</span></em> ' : '';
              $accountable = (in_array($task->user_id, json_decode($task->accountable))) ? '<em><span class="levels">Accountable</span></em> ' : '';
              $consulted = (in_array($task->user_id, json_decode($task->consulted))) ? '<em><span class="levels">Consulted</span></em> ' : '';
              $informed = (in_array($task->user_id, json_decode($task->informed))) ? '<em><span class="levels">Informed</span></em> ' : '';

              $due_date = mdate('%M %d, %Y',strtotime($task->date_due));
              $priority;
              switch ($task->priority) {
                  case '1':
                    $priority = '<span class="label label-default">Low</span>';
                    break;
                  case '2':
                    $priority = '<span class="label label-warning">Normal</span>';
                    break;
                  case '3':
                    $priority = '<span class="label label-danger">High</span>';
                    break;
                }
              $action;
              if ($userid == $task->user_id) {
                $action = '<li><a href="'.$delete_task_url.'" rel="tooltip" class="btn btn-danger btn-xs" data-toggle="tooltip" data-placement="right" title="Delete Task"><i class="fa fa-times"></i></a></li>';
              }else{
                $action = '';
              }

              echo<<<TSK
              <tr>
                <td>$count</td>
                <td>
                  <strong>$title</strong>
                </td>
TSK;
              if ($responsible == '' || $accountable == '' || $consulted == '' || $informed  == '') {
                echo '<td class="table-roles">Owner</td>';
              }else{
                echo '<td class="table-roles">'.$responsible. $accountable. $consulted. $informed .'</td>';
              }
              echo<<<BTM
                <td class="table-roles">$responsible $accountable $consulted $informed</td>
                <td>$priority</td>
                <td>$due_date</td>
                <td>
              <div class="btn-group">
               <ul class="list-inline">
                <li>
                  <a href="$view_task_url" rel="tooltip" class="btn btn-primary btn-xs" data-toggle="tooltip" data-placement="left" title="View Task"><i class="fa fa-eye"></i></a>
                </li>
                <li>
                  <a href="$edit_task_url" rel="tooltip" class="btn btn-success btn-xs" data-toggle="tooltip" data-placement="top" title="Edit Task"><i class="fa fa-pencil"></i></a>
                </li>
                $action
                </ul>
              </div>
            </td>
              </tr>
BTM;

              }
                    // echo  '<td class="edit-row"><strong class="table-title text-warning">'.$title.'</strong><a href="'.$url.'" class="view">View</a> <a href="#">|</a> <a href="/edit-task" class="edit_link">Edit</a></td>';


          } else{
            echo '<p class="bg-success">There are no tasks for you right now so... Find some different work to do you bum.</p>';
          }
          ?>
        </tbody>
      </table>
    </div>
  </div>
</div>